
package com.lt.test.provider.dao.impl;

import com.lt.test.common.util.StringUtils;
import com.lt.test.provider.dao.BaseDaoSupport;
import com.lt.test.provider.dao.KtSupplierProdDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Repository
public class KtSupplierProdDaoImpl extends BaseDaoSupport implements KtSupplierProdDao {

	private static final Logger log = Logger.getLogger(KtSupplierProdDaoImpl.class);

	/**
	 * 根据查询条件获取实现集
	 */
	public List query(Map parameter) {
		List list = new ArrayList();
		try {
			list = this.queryForList("queryKtSupplierProds",parameter);
		} catch (Exception e) {
			log.error("错误:");
			e.printStackTrace();
		}
	    return list;
	}


	/**
	 * 创建实体
	 */
    public int create(Map parameter)
    {
    	return this.insert("createKtSupplierProd", parameter);
    }

    /**
     * 更新实体
     */
    public int update(Map parameter)
    {
    	return this.update("updateKtSupplierProd",parameter);
    }

    /**
     * 保存实体
     */
    public int save(Map parameter)
    {
    	return this.update("saveKtSupplierProd",parameter);
    }


     /**
     * 删除实体
     */
    public int delete(java.lang.String id)
    {
    	return this.delete("deleteKtSupplierProd",id);
    }
    /**
	 * 根据主键获取实体信息
	 */
	public Map findById(java.lang.String id) {
		return this.queryForSingle("queryKtSupplierProd", id);
	}

  	/**
     * 批量插入
     */
	public int batch(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.insert("batchKtSupplierProd",values);
  		return iEffectedRow;
	}

  	/**
     * 批量更新
     */
  	public int batchUpdate(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.update("batchUpdateKtSupplierProd",values);
  		return iEffectedRow;
	}

  	/**
     * 批量删除
     */
  	public int batchDelete(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.delete("batchDeleteKtSupplierProd",values);
  		return iEffectedRow;
	}


	@Override
	public List<Map<String, Object>> getSupplierProdList(String supplierId, String keyword, Long beginNum, Long endNum) {
		// TODO Auto-generated method stub
		StringBuilder sqlStr = new StringBuilder();
		LinkedList<Object> params = new LinkedList<Object>();
		sqlStr.append("SELECT * FROM (SELECT t.*,ROWNUM rn FROM ( ");
		sqlStr.append("SELECT T1.PRODUCT_CODE,T1.PRODUCT_NAME,T1.UNIT,T1.SPEC,t1.SELF_CODE,t1.HELP_CODE,t1.SHORT_CODE,t1.SHORT_NAME,t1.prod_type_id,t1.create_time,t1.create_user,t1.modify_time,t1.modify_user, T2.CST_PRICE SUPPLIER_PRICE,T3.BRAND_NAME,t4.TEXT PRODUCT_TYPE_NAME,T2.SUPPLIER_ID,T5.SUPPLIER_NAME,T6.TYPE_NAME,T2.ID RECORD_ID from D_PROD t1 LEFT JOIN KT_PROD_BRAND t3 ON T1.BRAND_ID = T3.BRAND_CODE LEFT JOIN KT_DICT t4 ON t1.product_type = t4.code AND t4.idx_code = 'BASE_PRODUCT_TYPE' LEFT JOIN KT_PROD_TYPE T6 ON T1.PROD_TYPE_ID = T6.TYPE_ID ,KT_SUPPLIER_PROD t2,KT_SUPPLIER T5 WHERE T1.PRODUCT_CODE = T2.PRODUCT_CODE AND T2.SUPPLIER_ID = T5.ID  AND T2.STATE = '1' AND T2.SUPPLIER_ID = ? ");
		params.add(supplierId);
		//		sqlStr.append(" LEFT JOIN KT_DICT t2 ON t1.OPERATOR_WAY = T2.CODE AND T2.STATE = '1' AND T2.IDX_CODE='SUPPLIER_OPERATOR_WAY' ");
//		sqlStr.append(" LEFT JOIN KT_DICT t3 ON T1.SETTLEMENT_WAY = T3.CODE  AND t3.STATE = '1' AND t3.IDX_CODE='SUPPLIER_SETTLEMENT_WAY' ");
//		sqlStr.append(" LEFT JOIN KT_DICT t4 ON T1.DEFAULT_PURCHASE_WAY = T4.CODE  AND t4.STATE = '1' AND t4.IDX_CODE='SUPPLIER_DEFAULT_PURCHASE_WAY' ");
//		sqlStr.append(" LEFT JOIN KT_DICT t5 ON T1.REGISTER_WAY = T5.CODE  AND t5.STATE = '1' AND t5.IDX_CODE='SUPPLIER_REGISTER_WAY' ");
//		sqlStr.append(" WHERE 1 = 1");
		if(!StringUtils.isNullOrEmpty(keyword)){
			sqlStr.append(" AND (T1.PRODUCT_CODE like ?  OR T1.PRODUCT_NAME LIKE ? OR T5.SUPPLIER_NAME LIKE ? ) ");
			params.add("%" + keyword + "%");
			params.add("%" + keyword + "%");
			params.add("%" + keyword + "%");
		}
		sqlStr.append(" ORDER BY T1.CREATE_TIME,T1.MODIFY_TIME DESC)t WHERE  ? >=ROWNUM )t WHERE   t.rn >=? ");
		params.add(endNum);
		params.add(beginNum);

		return this.queryAsArray(sqlStr.toString(), params.toArray());
	}


	@Override
	public Map<String, Object> getSupplierProdListSize(String supplierId, String keyword) {
		// TODO Auto-generated method stub
		StringBuilder sqlStr = new StringBuilder();
		LinkedList<Object> params = new LinkedList<Object>();
		sqlStr.append("SELECT count(1) total from D_PROD t1,KT_SUPPLIER_PROD t2,KT_SUPPLIER T5 WHERE T1.PRODUCT_CODE = T2.PRODUCT_CODE AND T2.SUPPLIER_ID = T5.ID  AND T2.STATE = '1' AND T2.SUPPLIER_ID = ? ");
		params.add(supplierId);
		if(!StringUtils.isNullOrEmpty(keyword)){
			sqlStr.append(" AND (T1.PRODUCT_CODE like ?  OR T1.PRODUCT_NAME LIKE ? OR T5.SUPPLIER_NAME LIKE ? ) ");
			params.add("%" + keyword + "%");
			params.add("%" + keyword + "%");
			params.add("%" + keyword + "%");
		}
		return this.query(sqlStr.toString(), params.toArray());
	}

	@Override
	public Map<String, Object> getRecordBySupplierIdAndProductCode(String supplierId, String productCode) {
		// TODO Auto-generated method stub
		StringBuilder sqlStr = new StringBuilder();
		sqlStr.append("SELECT * FROM KT_SUPPLIER_PROD WHERE SUPPLIER_ID = ? AND PRODUCT_CODE = ? AND STATE = '1'");
		return this.query(sqlStr.toString(), new Object[]{supplierId,productCode});
	}
}
